package com.android.tiango.baba.act;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;

import com.android.tiango.baba.BaBaseActivity;


public class ActPhotoCamera extends BaBaseActivity {

	
	private static int CAMERA = 0101;
	private int ScreenW;
	private int ScreenH;
	private String fileName;
	private static int CROP = 0102;
	
	private String fileName2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		initView();
		ScreenW = getWindowManager().getDefaultDisplay().getWidth();
		ScreenH = getWindowManager().getDefaultDisplay().getHeight();
	}

	private void initView() {
		// TODO Auto-generated method stub
		
		File file = new File(Environment.getExternalStorageDirectory().getPath()+"/TianGouPicture/");  
        file.mkdirs();// 创建文件夹  
		String name = DateFormat.format("yyyyMMdd_hhmmss",
				Calendar.getInstance(Locale.CHINA))
				+ ".jpg";
		fileName = Environment.getExternalStorageDirectory().getPath()
				+ "/TianGouPicture/" + name;
		Intent camera = new Intent("android.media.action.IMAGE_CAPTURE");
		camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(fileName)));
		startActivityForResult(camera, CAMERA);
	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CAMERA && resultCode == Activity.RESULT_OK
				) {
			startPhotoZoom(fileName);
		}else if(requestCode == CROP && resultCode == Activity.RESULT_OK){
			Intent intent = new Intent(this, ActFilter.class);
			//intent.setData(Uri.fromFile(new File(fileName)));
			intent.putExtra("name", fileName2);
			startActivity(intent);
			finish();
		}
	}

	/**
	 * 调用android系统自带的图片裁剪，把用户选择的图片 裁剪成适合用户手机的大小
	 * 
	 * @param imagePath
	 */
	public void startPhotoZoom(String imagePath) {
		// 获得裁剪图片的路径文件
		File file = new File(imagePath);
		// 获得裁剪图片的名字
		String originName = file.getName();
		// 获得裁剪图片的uri
		Uri uri = Uri.fromFile(file);
		// 调用intent跳转的裁间页面
		Intent intent = new Intent("com.android.camera.action.CROP");
		// 传入裁剪图片的路径uri和需要裁剪的格式
		intent.setDataAndType(uri, "image/*");
		// 可裁剪状态
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", ScreenW);
		intent.putExtra("aspectY", ScreenH / 4);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", ScreenW);
		intent.putExtra("outputY", ScreenH / 4);
		intent.putExtra("return-data", true);
		
		String name = DateFormat.format("yyyyMMdd_hhmmss",
				Calendar.getInstance(Locale.CHINA))
				+ ".jpg";
		fileName2 = Environment.getExternalStorageDirectory().getPath()
				+ "/TianGouPicture/" + name;
		
		intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(fileName2)));
		startActivityForResult(intent, CROP);
	}
}
